package io.babystar.modules.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.R;
import io.babystar.modules.business.entity.BaseMemberCardEntity;
import io.babystar.modules.business.entity.CourseEntity;
import io.babystar.modules.business.entity.ExcelStudent;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.service.BaseMemberCardService;
import io.babystar.modules.business.service.CourseService;
import io.babystar.modules.business.service.ShopService;
import io.babystar.modules.business.service.StudentService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("business")
public class ExcelImportController {

    private static final Logger log = LoggerFactory.getLogger(ExcelImportController.class);
    @Autowired
    private StudentService studentService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private BaseMemberCardService baseMemberCardService;

    /**
     * 学生表表格导入
     * @param file
     * @return
     */
    @RequestMapping("/excelImportStudent")
    public R excelImportStudent(@RequestParam("file") MultipartFile file, @RequestParam String shopId) {
        if (shopId == null || "undefined".equals(shopId) || "请选择校区".equals(shopId)) {
            return R.error("请选择校区");
        }
        ImportParams importParams = new ImportParams();
        // 表格内容转为list
        ExcelImportResult<ExcelStudent> result = null;
        try {
            result = ExcelImportUtil.importExcelMore(file.getInputStream(), ExcelStudent.class,
                    importParams);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取excel数据失败，excel里面有不规范数据，重点查看日期类型！" + e.toString());
        }

        // 成功读出的学生信息list
        List<ExcelStudent> successList = result.getList();
        //读取店铺下所有课程列表
        List<CourseEntity> courseEntityList = courseService.list(
                new QueryWrapper<CourseEntity>()
                        .eq("shopId", shopId)
        );
        ShopEntity shopEntity = (ShopEntity) shopService.getOne(
                new QueryWrapper<ShopEntity>()
                        .select("organizeId")
                        .eq("id", shopId)
        );
        Integer organizeId = shopEntity.getBusinessId();
        //会员卡列表
        List<BaseMemberCardEntity> memberCardEntityList = baseMemberCardService.list(
                new QueryWrapper<BaseMemberCardEntity>()
                        .eq("organizeId", organizeId)
        );
        List<ExcelStudent> rsList = studentService.importStudentInfo(successList, courseEntityList, memberCardEntityList, shopId, organizeId);

        return R.ok().put("rsList", rsList);
    }



}

